﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using System.Data;
using Runda.CMS.Common.Utility;
using System.Text;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Finiance
{
    public partial class SalerMonthCheckOffs : BasePage
    {
        public Guid CheckOffsID
        {
            get
            {
                if (hiddenCheckOffsID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenCheckOffsID.Value);
            }
            set
            {
                hiddenCheckOffsID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Request.QueryString["CheckOffsID"] != null)
                {
                    CheckOffsID = new Guid(Request.QueryString["CheckOffsID"]);
                }
                

                for (int index = System.DateTime.Now.Year - 5; index < 2050; index++)
                {
                    ddlYear.Items.Add(new ListItem(index.ToString() + "年", index.ToString()));
                }
                for (int index = 1; index < 13; index++)
                {
                    ddlMonth.Items.Add(new ListItem(index.ToString() + "月", index.ToString()));
                }
                ddlYear.SelectedValue = System.DateTime.Now.Year.ToString();
                ddlMonth.SelectedValue = System.DateTime.Now.Month.ToString();
                List<SysDictionaryEntity> list = new SysDictionaryBll().GetAll<SysDictionaryEntity>(" and Type=" + System.Convert.ToInt32(ContractEnumerator.FinianceAccountsReceivable.CheckOffsType).ToString());
                if (list != null && list.Count > 0)
                {
                    for (int index = 0; index < list.Count; index++)
                    {
                        ddlCheckOffsType.Items.Add(new ListItem(list[index].Code, list[index].ID.ToString()));
                    }
                }
                if (Request.QueryString["UserID"] != null && Request.QueryString["UserID"] != "")
                {
                    txtUserID.Value = Request.QueryString["UserID"];

                    List<ArmUserPerformanceEntity> userPerformanceEntitys = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and UserID = '" + txtUserID.Value + "' and Month = " + System.DateTime.Now.Month.ToString() + " and Year = " + System.DateTime.Now.Year.ToString());

                    List<ArmCheckOffsForMonthEntity> monthCheckTotals = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and UserID = '" + txtUserID.Value + "' and Year =" + System.DateTime.Now.Year + " and Month =" + System.DateTime.Now.Month);

                    txtMonthFunds.Text = "0";

                    decimal monthCheckTotalsFund = 0;
                    if (monthCheckTotals != null && monthCheckTotals.Count > 0)
                    {
                        foreach (ArmCheckOffsForMonthEntity monthCheckTotal in monthCheckTotals)
                        {
                            monthCheckTotalsFund += monthCheckTotal.RealizeFunds;
                        }
                    }
                    if (userPerformanceEntitys != null && userPerformanceEntitys.Count > 0)
                    {
                        decimal total = 0;
                        foreach (ArmUserPerformanceEntity entity in userPerformanceEntitys)
                        {
                            total = total + entity.Fine + entity.Reward;
                        }
                        txtMonthFunds.Text = (total - monthCheckTotalsFund).ToString();
                    }

                    List<ArmUserPerformanceEntity> fundsTotals = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and UserID = '" + txtUserID.Value + "' and Year <=" + System.DateTime.Now.Year + " and Month <" + System.DateTime.Now.Month);
                    List<ArmCheckOffsForMonthEntity> realizedFundsTotals = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and UserID = '" + txtUserID.Value + "'        and Year <=" + System.DateTime.Now.Year + " and Month <" + System.DateTime.Now.Month);
                    decimal realizedFundsTotal = 0;
                    decimal fundsTotal = 0;
                    foreach (ArmUserPerformanceEntity entity in fundsTotals)
                    {
                        fundsTotal = fundsTotal + entity.Reward + entity.Fine;
                    }
                    foreach (ArmCheckOffsForMonthEntity entity in realizedFundsTotals)
                    {
                        realizedFundsTotal = realizedFundsTotal + entity.RealizeFunds;
                    }

                    txtUnrealizedFundsTotal.Text = (fundsTotal - realizedFundsTotal).ToString();
                    
                }

                SystemBll bll = new SystemBll();
                ArmCheckOffsForMonthEntity entity1 = new ArmCheckOffsForMonthEntity();
                if (CheckOffsID != Guid.Empty)
                {
                    entity1 = bll.Get<ArmCheckOffsForMonthEntity>(CheckOffsID);
                    if (entity1 != null)
                    {
                        txtComment.Text = entity1.Comment;
                        txtCheckOffInv.Text = entity1.CheckOffInv;
                        ddlMonth.SelectedValue = entity1.Month.ToString();
                        ddlYear.SelectedValue = entity1.Year.ToString();
                        txtRealizeFunds.Value = System.Convert.ToDouble(entity1.RealizeFunds);
                        txtUnrealizedFunds.Text = entity1.UnrealizedFunds.ToString("N");
                        ddlCheckOffsType.SelectedValue = entity1.CheckOffsType.ToString();
                        ddlYear.Enabled = false;
                        ddlMonth.Enabled = false;
                        txtRealizeFunds.ReadOnly = true;
                        txtCheckOffInv.ReadOnly = true;
                    }
                }
                
            }
        }

        private bool save()
        {       
            SystemBll bll = new SystemBll();
            ArmCheckOffsForMonthEntity entity = new ArmCheckOffsForMonthEntity();
            if (CheckOffsID != Guid.Empty)
            {
                entity = bll.Get<ArmCheckOffsForMonthEntity>(CheckOffsID);
                entity.CheckOffsType = new Guid(ddlCheckOffsType.SelectedValue);
                entity.Comment = txtComment.Text.Trim();
            }
            else
            {
                entity.CreatedOn = System.DateTime.Now;
                entity.CheckOffsID = Guid.NewGuid();
                entity.CheckOffsType = new Guid(ddlCheckOffsType.SelectedValue);
                entity.Comment = txtComment.Text.Trim();
                entity.CheckOffInv = txtCheckOffInv.Text.Trim();
                entity.CreatedBy = CurrentUserID;
                entity.Month = System.Convert.ToInt32(ddlMonth.SelectedValue);
                entity.RealizeFunds = System.Convert.ToDecimal(txtRealizeFunds.Value);
                entity.UnrealizedFunds = System.Convert.ToDecimal(txtMonthFunds.Text) - System.Convert.ToDecimal(txtRealizeFunds.Value);
                entity.UserID = new Guid(txtUserID.Value);
                entity.Year = System.Convert.ToInt32(ddlYear.SelectedValue);
                
            }
            new ArmCheckOffsForMonthBll().InsertOrUpdate(entity);
            return true;
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (txtCheckOffInv.Text.Trim() != "")
            {
                if (new BillBll().IsExistsInv(txtCheckOffInv.Text.Trim()) == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('发票号不存在，请重新输入');", true);
                    return;
                }
            }
            if (save())
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
            }
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ArmUserPerformanceEntity> userPerformanceEntitys = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and UserID = '" + txtUserID.Value + "' and Month = " + ddlMonth.SelectedValue + " and Year = " + ddlYear.SelectedValue);
            List<ArmCheckOffsForMonthEntity> monthCheckTotals = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and UserID = '" + txtUserID.Value + "' and Year =" + ddlYear.SelectedValue + " and Month =" + ddlMonth.SelectedValue); 
            decimal monthCheckTotalsFund = 0;
            if (monthCheckTotals != null && monthCheckTotals.Count > 0)
            {
                foreach (ArmCheckOffsForMonthEntity monthCheckTotal in monthCheckTotals)
                {
                    monthCheckTotalsFund += monthCheckTotal.RealizeFunds;
                }
            }
            txtMonthFunds.Text = "0";
            if (userPerformanceEntitys != null && userPerformanceEntitys.Count > 0)
            {
                decimal total = 0;
                foreach (ArmUserPerformanceEntity entity in userPerformanceEntitys)
                {
                    total = total + entity.Fine + entity.Reward;
                }
                txtMonthFunds.Text = (total - monthCheckTotalsFund).ToString();
            }
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ArmUserPerformanceEntity> userPerformanceEntitys = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and UserID = '"+txtUserID.Value+"' and Month = "+ddlMonth.SelectedValue + " and Year = "+ ddlYear.SelectedValue);
            List<ArmCheckOffsForMonthEntity> monthCheckTotals = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and UserID = '" + txtUserID.Value + "' and Year =" + ddlYear.SelectedValue + " and Month =" + ddlMonth.SelectedValue);
            decimal monthCheckTotalsFund = 0;
            if (monthCheckTotals != null && monthCheckTotals.Count > 0)
            {
                foreach (ArmCheckOffsForMonthEntity monthCheckTotal in monthCheckTotals)
                {
                    monthCheckTotalsFund += monthCheckTotal.RealizeFunds;
                }
            }
            txtMonthFunds.Text = "0";
            if (userPerformanceEntitys != null && userPerformanceEntitys.Count>0)
            {
                decimal total = 0;
                foreach (ArmUserPerformanceEntity entity in userPerformanceEntitys)
                {
                    total = total + entity.Fine + entity.Reward;
                }
                txtMonthFunds.Text = (total - monthCheckTotalsFund).ToString();
            }
        }

        protected void txtRealizeFunds_TextChanged(object sender, EventArgs e)
        {
            txtUnrealizedFunds.Text = (System.Convert.ToDecimal(txtMonthFunds.Text) - System.Convert.ToDecimal(txtRealizeFunds.Value)).ToString();
        }
    }
}
